package com.atguigu.nio;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author: 叶子
 * @date: 2022/01/17 16:56
 * 本地文件读数据
 **/
public class NIOFileChannel02 {
    public static void main(String[] args) throws IOException {
//        创建文件的输入流
        File file = new File("d:\\file01.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
//        通过fileInputStream获取对应的FileChannel ->实际类型 FileChannelImpl
        FileChannel fileChannel = fileInputStream.getChannel();
//        创建缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate((int) file.length());
//        将通道的数据读入到缓冲区
        fileChannel.read(byteBuffer);
//        将缓冲区的字节数据转换成String
        System.out.println(byteBuffer);//java.nio.HeapByteBuffer[pos=12 lim=12 cap=12]
        System.out.println(byteBuffer.array());//[B@4554617c
        System.out.println(new String(byteBuffer.array()));//hello,阿琳
        fileInputStream.close();
    }
}